package android.vstore.activities;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import DAO.ProductDAO;
import DTO.PInfo;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.text.format.Time;
import anroid.vstore.provider.Constant;
import anroid.vstore.provider.Helper;
import anroid.vstore.provider.SharedPreference;

import com.appvn2014.appstorevn.GCMRegister;
import com.appvn2014.appstorevn.R;
import com.appvn2014.appstorevn.R.layout;
import com.appvn2014.appstorevn.R.string;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;

public class SceneLoadingActivity extends FragmentActivity {

    private final int SPLASH_DISPLAY_LENGHT = 3000;            //set your time here......

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressWarnings("deprecation")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scene_loading);

//        if(android.os.Build.VERSION.SDK_INT > 8)
//        {
//        	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//        	StrictMode.setThreadPolicy(policy); 
//        }
		DateFormat df = new SimpleDateFormat("dd MM yyyy");
		
		SharedPreference sharedpref = new SharedPreference(this);
		String lastestDate = sharedpref.getLastestDate();
		Date currDate = Calendar.getInstance().getTime();
		if(lastestDate.equals(df.format(currDate)))
			MainActivity.isCheckedNotification = true;
		else
			MainActivity.isCheckedNotification = false;
		
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .threadPoolSize(3) // default
        .threadPriority(Thread.NORM_PRIORITY - 1) // default
        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
//        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
        .memoryCacheSize(2 * 1024 * 1024)
        .memoryCacheSizePercentage(13) // default
//        .diskCache(new UnlimitedDiscCache(cacheDir)) // default
        .diskCacheSize(50 * 1024 * 1024)
        .diskCacheFileCount(100)
        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
        .writeDebugLogs()
    	.build();
    	ImageLoader.getInstance().init(config);
        if(Helper.haveNetworkConnection(this))
        {
        	GCMRegister.Register(this);
        	
        	(new Loader()).execute();
        }else
        {
        	AlertDialog ad = new AlertDialog.Builder(this).create();
        	ad.setCancelable(false); // This blocks the 'BACK' button
        	ad.setMessage(getString(R.string.load_scene_activity_not_connect_internet));
        	ad.setButton("OK", new DialogInterface.OnClickListener() {
        		@Override
        		public void onClick(DialogInterface dialog, int which) {
        			finish();                   
        		}
        	});
        	ad.show();
        }
    }
//    private boolean checkPlayServices() {
//        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
//        if (resultCode != ConnectionResult.SUCCESS) {
//            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
//                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
//                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
//            } else {
//                Log.i(TAG, "This device is not supported.");
//                finish();
//            }
//            return false;
//        }
//        return true;
//    }
    public class Loader extends AsyncTask<String, Void, ArrayList<PInfo>>{
		@Override
		protected ArrayList<PInfo> doInBackground(String... params) {
			
//			ArrayList<PInfo> lstProds = Helper.getPackages(SceneLoadingActivity.this);
			MainActivity._appcategories = ProductDAO.GetCats(Constant.TYPE_APPLICATION);
			MainActivity._gamecategories = ProductDAO.GetCats(Constant.TYPE_GAME);
			return null;
		}      
		
		@Override
		protected void onPostExecute(ArrayList<PInfo> lstVipProd) {
			MainActivity._installed = lstVipProd;
			Intent mainIntent = new Intent(SceneLoadingActivity.this,MainActivity.class);
			if(SceneLoadingActivity.this.getIntent().getExtras() != null)
				mainIntent.putExtras(SceneLoadingActivity.this.getIntent().getExtras());
			
            SceneLoadingActivity.this.startActivity(mainIntent);
            SceneLoadingActivity.this.finish();
		}

		@Override
		protected void onPreExecute() {
			
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}

}
